﻿using System;
using System.Collections.Generic;
using System.Linq;

namespace InfiniTec.Collections.Generic
{
	public static class CollectionExtension
	{
		public static void AddRange<T>(this ICollection<T> collection, IEnumerable<T> items)
		{
			if (collection == null) throw new ArgumentNullException("collection");
			if (items == null) throw new ArgumentNullException("items");

			items.Foreach(collection.Add);
		}

        public static void RemoveAll<T>(this ICollection<T> collection, IEnumerable<T> items)
        {
            if (collection == null) throw new ArgumentNullException("collection");
            if (items == null) throw new ArgumentNullException("items");

            foreach (var item in items)
            {
                collection.Remove(item);
            }
        }

        public static void RemoveAll<T>(this ICollection<T> collection, Func<T, bool> predicate)
        {
            if (collection == null) throw new ArgumentNullException("collection");

            var itemsToDelete = collection.Where(predicate).ToArray();

            foreach (var item in itemsToDelete)
            {
                collection.Remove(item);
            }
        }

        public static void RemoveAll<T>(this IList<T> collection, Func<T, bool> predicate)
        {
            if (collection == null) throw new ArgumentNullException("collection");

            for (int i = collection.Count - 1; i >= 0; i--)
            {
                if (predicate(collection[i]))
                {
                    collection.RemoveAt(i);
                }
            }
        }
	}
}
